<!-- 费用计算 -->
<template>
  <div class="app-container">
    <!-- header -->
    <el-card>
      <div slot="header" class="header">
        <span>代理费提取计算</span>
      </div>
      <div style="marginTop:20px" class="filter-container">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="月份">
            <el-date-picker v-model="searchData.month" type="month" placeholder="选择月" value-format="yyyy-MM">
            </el-date-picker>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onClickSearch">查询</el-button>
        </el-form>
      </div>
    </el-card>
    <!-- body -->
    <el-card style="marginTop:20px">
      <div class="table-wrapper">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="流水号" width="100" />
          <el-table-column prop="month" label="月份" width="150" />
          <el-table-column prop="agentProfitStatus" label="代理费统计状态" min-width="180">
            <template slot-scope="scope">
              <el-tag :type="scope.row.agentProfitStatus | tagTypeFilter">
                {{ scope.row.agentProfitStatus | tagTextFilter }}
              </el-tag>
              <el-button v-if="btnVisibleFilter(scope.row)" @click="onClickCalcProfit(scope.row,'agent')" size="small" type="primary">
                {{ scope.row.agentProfitStatus | btnTextFilter }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="leadProfitStatus" label="领导奖统计状态" min-width="180">
            <template slot-scope="scope">
              <el-tag :type="scope.row.leadProfitStatus | tagTypeFilter">
                {{ scope.row.leadProfitStatus | tagTextFilter }}
              </el-tag>
              <el-button v-if="btnVisibleFilter(scope.row)" @click="onClickCalcProfit(scope.row,'lead')" size="small" type="primary">
                {{ scope.row.leadProfitStatus | btnTextFilter }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column prop="enterProfitStatus" label="收益入账状态" min-width="180">
            <template slot-scope="scope">
              <el-tag :type="scope.row.enterProfitStatus | tagTypeFilter">
                {{ scope.row.enterProfitStatus | tagTextFilter }}
              </el-tag>
              <el-button v-if="btnVisibleFilter(scope.row)" @click="onClickCalcProfit(scope.row,'enter')" size="small" type="primary">
                {{ scope.row.enterProfitStatus | btnTextFilter }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination background layout="prev, pager, next" @current-change="currentChange" :page-count="Math.ceil(this.pageInfo.total / this.pageInfo.pageSize)"></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      pageInfo: {
        pageNum: 1, // 当前页码
        pageSize: 10, // 展示数量
        total: 1, // 总记录数
      },
      tableData: [],

      searchData: {
        nickName: '',
        month: '',
      },
    };
  },
  created() {},
  mounted() {
    this.getProcessList();
  },
  computed: {},
  methods: {
    btnVisibleFilter({ agentProfitStatus, enterProfitStatus }) {
      if (enterProfitStatus === 'S') return false;

      switch (agentProfitStatus) {
        case 'S':
        case 'I':
        case 'F':
          return true;
        case 'R':
        default:
          return false;
      }
    },

    /**
     * 搜索
     */
    onClickSearch() {
      this.pageInfo.pageNum = 1;
      this.getProcessList();
    },

    /**
     * 当前页变更
     */
    currentChange(val) {
      this.pageInfo.pageNum = val;
      this.getMonthProfitList();
    },

    onClickCalcProfit({ id }, type) {
      switch (type) {
        case 'agent':
          this.calcAgentProfit(id);
          break;
        case 'lead':
          this.calcLeadProfit(id);
          break;
        case 'enter':
          this.calcEnterProfit(id);
          break;
        default:
          break;
      }
    },
    /**
     * S:已处理，计算成功
     * I:未处理，
     * R:处理中
     * 收益入账状态为S时，不做任何操作
     */
    async getProcessList() {
      const params = {
        ...{
          pageNum: this.pageInfo.pageNum,
          pageSize: this.pageInfo.pageSize,
          needTotalCount: true,
        },
        ...this.searchData,
      };
      const response = await this.$api.getProcessList(params);
      this.tableData = response.data;
      this.pageInfo.total = response.total;
    },

    async calcAgentProfit(id) {
      const params = { id };
      const response = await this.$api.calcAgentProfit(params);
      if (response.success) {
        this.$message.success('操作成功');
        this.getProcessList();
      }
    },

    async calcLeadProfit(id) {
      const params = { id };
      const response = await this.$api.calcLeadProfit(params);
      if (response.success) {
        this.$message.success('操作成功');
        this.getProcessList();
      }
    },

    async calcEnterProfit(id) {
      const params = { id };
      const response = await this.$api.calcEnterProfit(params);
      if (response.success) {
        this.$message.success('操作成功');
        this.getProcessList();
      }
    },
  },
  filters: {
    tagTypeFilter(status) {
      switch (status) {
        case 'S':
          return 'success';
        case 'I':
          return 'info';
        case 'R':
          return 'warning';
        case 'F':
          return 'danger';
        default:
          return '';
      }
    },
    tagTextFilter(status) {
      switch (status) {
        case 'S':
          return '处理完成';
        case 'I':
          return '未开始';
        case 'R':
          return '处理中';
        case 'F':
          return '处理失败';
        default:
          return '';
      }
    },
    btnTextFilter(status) {
      switch (status) {
        case 'S':
          return '重新计算';
        case 'I':
          return '计算';
        case 'F':
          return '重新计算';
        default:
          return '';
      }
    },
  },
};
</script>

<style lang='scss' scoped>
</style>
